<?php
declare (strict_types = 1);

namespace app\api\controller\order;

use app\BaseController;
use app\common\lib\Show;
use think\facade\Request;
use app\common\business\Order;

class Index extends BaseController
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save()
    {
        $data = [
            'address_id' => Request::param('address_id', 0, 'intval'),
            'ids'        => Request::param('ids', null, 'trim'),
            'user_id'    => (int) Request::header('userId')
        ];
        if (!$data['address_id'] || !$data['ids']) return Show::error('参数错误');

        try {
            $order = new Order();
            $edit  = $order->addOrder($data);
        } catch (\Exception $e) {
            return Show::error($e->getMessage());
        }
        return $edit ? Show::success($edit, '订单提交成功') : Show::error('订单提交失败');
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read(int $id)
    {
        $condition = [
            'user_id' => Request::header('userId'),
            'order_id' => $id
        ];
        if (!$condition['order_id']) return Show::error('参数错误');
        $order = new Order();
        $result = $order->getOrderByOrderId($condition);
        return $result ? Show::success($result) : Show::error();
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
